import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Scanner;

public class ShowException {
    public static int getData() {
        Scanner sc = null;
        try {
            sc = new Scanner(System.in);
            int data = sc.nextInt();
            return data;//抛出异常后这条语句就不会被执行
        } catch (InputMismatchException e) {
            e.printStackTrace();
        } finally {
            System.out.println("finally中代码");
            sc.close();
        }
        System.out.println("try-catch-finally之后代码");

        return 0;
    }

    public static void main(String[] args) {
        int data = getData();
        System.out.println(data);
    }









    public static void main3(String[] args) {
        int[] arr1={1,2,3,4,5};
        try{
            func1(arr1,5);
        }catch(NullPointerException e){
            e.printStackTrace();
            System.out.println("数组越界异常。。。");
        }finally{ //finally不能单独使用，要和try-catch配合使用。
            System.out.println("执行finally.....");
        }
        System.out.println("执行try-catch-finally外的代码.....");
    }

    public static int func1(int[] arr,int index){
        return arr[index];
    }


    public static void main1(String[] args) {
        int[] arr1={1,2,3,4,5};
        int[] arr2=null;
        try{
            func1(arr1,5);
        }catch(NullPointerException e){
            System.out.println("空指针异常。。。。");
        }catch(ArrayIndexOutOfBoundsException e){
            System.out.println("数组越界异常。。。");
        }catch(Exception e){
            e.printStackTrace();
        }
    }

    public static void main2(String[] args) {
        int[] arr1={1,2,3,4,5};
        int[] arr2=null;
        try{
            func1(arr1,5);
            func1(arr2,1);
        }catch(Exception e){
            e.printStackTrace();
        }
    }


}
